export default {
    setCity(state, city) { //设置城市ID
        state.city = city
    },
    setCityID(state, id) { //设置城市ID
        state.id = id
    },
    setGeohash(state, geohash) {//经纬度
        state.geohash = geohash
    },
    setLatitude(state, latitude) {//经纬度
        state.latitude = latitude
    },
    setLongitude(state, longitude) {//经纬度
        state.longitude = longitude
    },
    setUser(state, user) { //设置用户信息
        state.user = user
        state.isLogin = true
    },
    outUser(state) { //退出登录
        state.user = null
        state.isLogin = false
    },
    addCar(state, shopInfo) {
        let flag = false;
        // state.car.some(item => {
        //     if (item.id == shopInfo.id) { //如果仓库中有该商品加1
        //         item.count += shopInfo.count
        //         flag = true
        //         return true
        //     }
        // })
        state.car.forEach(ele => {
            if (ele.id == shopInfo.id) { //如果仓库中有该商品加
                ele.count += parseInt(shopInfo.count)
                flag = true
                return true
            }
        })
        if (!flag) { //如果没有就创建该商品
            state.car.push(shopInfo)
        }
    },
    setCarCount(state, info) { //购物车改变商品数量
        state.car.forEach(e => {
            if (e.id === info.id) {
                e.count = info.count
            }
        })
    },
    removeCar(state, id) { //删除商品
        state.car.forEach((e, i) => {
            if (e.id === id) {
                state.car.splice(i, 1)
                return;
            }
        })
    },
    changeCarState(state, info) { //改变商品选中状态
        state.car.forEach(e => {
            if (e.id === info.id) {
                e.select = info.state
                return;
            }
        })
    },
    removeSelect(state) { //购买完商品，删除已经选中的商品
        let car = []
        state.car.forEach(e => {
            if (!e.select) {
                car.push(e)
            }
        })
        state.car = car;
    },
    saveSearch(state, search) { //保存历史记录
        let flag = true
        state.search.forEach(e => {
            if (e.value == search.value) { //有记录
                flag = false
                return
            }
        })
        if (flag) {
            state.search.push(search)
        }
    },
    clearGroup(state, value) { //清空单个历史记录
        state.search.forEach((e, i) => {
            if (e.value == value) {
                state.search.splice(i, 1)
            }
        })
    },
    clearSearch(state) {//清空所有历史记录
        state.search = []
    }
}